#include "Queue.h"

void QueueInit(Queue* que){
     assert(que);
     que->phead=que->tail=NULL;
}
void QueueDestroy(Queue* que){
     assert(que);
     while(que->phead!=NULL){
          QueueNode* tmp=que->phead->next;
          free(que->phead);
          que->phead=tmp;
     }
     que->phead=que->tail=NULL;
}
void QueuePush(Queue* que,DataType x){
     assert(que);
     QueueNode* newnode=(QueueNode*)malloc(sizeof(QueueNode));
     newnode->x=x;newnode->next=NULL;
     if(que->phead==NULL){
        que->phead=que->tail=newnode; 
     }
     else{
        que->tail->next=newnode;
        que->tail=newnode;
     }
}
void QueuePop(Queue* que){
     assert(que);
     assert(que->phead);
     if(que->phead==que->tail){
        free(que->phead);
        que->phead=que->tail=NULL;
     }
     else{
        QueueNode* cur=que->phead->next;
        free(que->phead);
        que->phead=cur;
     }
}
bool QueueEmpty(Queue* que){
     assert(que);
     return que->phead==NULL;
}
int QueueSize(Queue* que){
     assert(que);
     int cur=0;QueueNode* tmp=que->phead;
     while(tmp!=NULL){
         cur++;
         tmp=tmp->next;
     }
     return cur;
}
DataType QueueFront(Queue* que){
    assert(que); 
    assert(que->phead);
    return que->phead->x;
}
DataType QueueBack(Queue* que){
    assert(que);
    assert(que->phead);
    return que->tail->x;
}
